package com.neueedu.android;

import android.app.ProgressDialog;
import android.content.Intent;
//import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.neueedu.android.concat.ConcatAddActivity;
import com.neueedu.android.concat.ConcatEditActitiy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ConcatActivity extends AppCompatActivity implements  AdapterView.OnItemClickListener {


    String types[] = new String[]{"成人","学生","老人","军人"};
    String names[] = new String[]{"邬昀华",
            "杨音好",
            "万靓侃",
            "章咪海",
            "包菊会",
            "柯宗福",
            "叶深宝",
            "萧嵘纳",
            "薄朴军",
            "裴俭杨",
            "申京姿",
            "裴曦尤",
            "郎建盟",
            "钱洁惟",
            "蒙向锬",
            "祝沛闯",
            "裴影霏",
            "鲍雪玥",
            "贺骊昭",
            "尹笑漫",
            "黎海荷",
            "仰岭冰",
            "晏剑伊",
            "吴河溢",
            "薛艺沁",
            "虞拓屹",
            "奚涓琴",
            "怀姗琦",
            "丁宝焰",
            "庞江歆",
            "顾耘畏"};

    Random rand = new Random();
    List<HashMap<String, Object>> data = new ArrayList<>();

    //ListView的数据

    SimpleAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_concat);


        //请求网络 http://192.168.70.101:8080/concat/list

       //1 找到  ListView
        ListView lvConcatList = findViewById(R.id.lvConcatList);


        //2 创建一个适配器  并绑定数据

//
//        for (int i = 0; i < 10; i++) {
//            HashMap<String,Object>  row = new HashMap<String,Object>();
//
//            row.put("index",i+1);
//            row.put("name",names[i]);
//            row.put("idno", UUID.randomUUID().toString());
//            row.put("type", types[rand.nextInt(types.length-1)]);
//
//            data.add(row);
//        }

        initData();


        String[] from =  new String[]{"index","name","idno","type"};
        int to[] = new int[]{R.id.index ,R.id.tvName,R.id.tvIdNo ,R.id.tvType };
        adapter = new SimpleAdapter(this,data ,  R.layout.concat_list_item, from,to);


        //3 将适配器和 ListView绑定
        lvConcatList.setAdapter(adapter);

        lvConcatList.setOnItemClickListener(this);

    }

    private void initData() {


        MyAsyncTask task = new MyAsyncTask();
        task .execute();



    }



    //异步任务 处理  网络请求  等 需要耗时的工作

    ProgressDialog progressDialog;
    class MyAsyncTask extends AsyncTask<String, String, List>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(ConcatActivity.this,"提示","加载中,请稍等",true,false);
            progressDialog.show();
        }

        @Override
        protected List doInBackground(String... strings) {

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder() .url("http://192.168.70.101:8080/concat/list").build();

            //同步的请求
            Response response  = null;
            List<HashMap> list = null;
            try {
                response = client.newCall(request).execute();

                String json = response.body().string();
                Gson gson = new Gson();
//                Gson gson = new GsonBuilder().create();
              list = gson.fromJson(json, new TypeToken<List<HashMap>>(){}.getType());

            } catch (IOException e) {
                e.printStackTrace();
            }


            return list;
        }

        @Override
        protected void onPostExecute(List list) {
            super.onPostExecute(list);

            data.clear();
            data.addAll(list);
            adapter.notifyDataSetChanged();
            progressDialog.dismiss();


        }


    }






    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        HashMap<String, Object> row = data.get(position);

        //跳转到  联系人编辑页面
        Intent intent = new Intent(this, ConcatEditActitiy.class);
        intent.putExtra("row",row);
        startActivity(intent);
    }


    //用于渲染菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {


//        MenuItem
        getMenuInflater().inflate(R.menu.concat_menu ,menu);

        return super.onCreateOptionsMenu(menu);
    }


    //监听菜单的事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.toAdd){

            //跳转到   添加的画面

            Intent intent = new Intent(this, ConcatAddActivity.class);

            startActivity(intent);

        }

        return false;
    }



}
